package com.example.demo.utils;

import java.io.*;
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;

/**
 * @Description:
 * @Author: WY
 * @CreateTime: 2019/5/30 20:56
 */
@Slf4j
public class FileUtil {
    private  static String FILE_PATH = "D:\\temp.jpg";
    private  static String FILE_TARGET_PATH = "D:\\target.jpeg";
    public static byte[] fileToBytes(String filePath) {
        byte[] buffer = null;
        File file = new File(filePath);

        FileInputStream fis = null;
        ByteArrayOutputStream bos = null;
        try {
            fis = new FileInputStream(file);
            bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            buffer = bos.toByteArray();
        } catch (FileNotFoundException ex) {
            log.error("file not found");
        } catch (IOException ex) {
            log.error("io excpetion");
        } finally {
            try {
                if (null != bos) {
                    bos.close();
                }
            } catch (IOException ex) {
                log.error(ex.getMessage());
            } finally{
                try {
                    if(null!=fis){
                        fis.close();
                    }
                } catch (IOException ex) {
                    log.error(ex.getMessage());
                }
            }
        }

        return buffer;
    }
    public static void bytesToFile(byte[] buffer, final String filePath){

        File file = new File(filePath);

        OutputStream output = null;
        BufferedOutputStream bufferedOutput = null;

        try {
            output = new FileOutputStream(file);

            bufferedOutput = new BufferedOutputStream(output);

            bufferedOutput.write(buffer);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            if(null!=bufferedOutput){
                try {
                    bufferedOutput.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(null != output){
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static byte[] imgCompress(byte[] sourceData){
        bytesToFile(sourceData,FILE_PATH);
        try {
            Thumbnails.of(FILE_PATH)
                    .scale(.5f)
                    .outputQuality(.5f)
                    .toFile(FILE_TARGET_PATH);
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] bytes = fileToBytes(FILE_TARGET_PATH);
        File file = new File(FILE_PATH);
        if(file.exists()){
            file.delete();
        }
        file = new File(FILE_TARGET_PATH);
        if(file.exists()){
            file.delete();
        }
        return bytes;
    }

}
